import React, { Component } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';

import { Form, Input } from 'antd';
import { SearchRow, SearchCol, SearchButton } from '@/styled/form';

import { DICT } from '../enum'

import { FormComponentProps } from "antd/lib/form/Form";
import { dealElement } from '@/utils/util';
import { DropDownSelectObj } from '@/components/DropDownSelect';

interface IProps extends FormComponentProps {
  current: number;
  form: any;
  dispatch?: ({ type }: { type: string, payload?: any }) => Promise<any>;
  initCurrent: Function;
  new: boolean;
  dict?: any;
  cond?: {
  };
}

const FormItem = Form.Item;

@connect(({ analyse_charge }) => ({
  dict: analyse_charge.dict,
  cond: analyse_charge.exceptionCond || {}
}))
class Search extends Component<IProps, any> {
  formRef: any;

  state = {
    show: false
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({ type: 'analyse_charge/queryDict', payload: { keyList: DICT } })
  }
  

  search = (e) => {
    e.preventDefault()
    const { form } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        const { dispatch } = this.props;
        dispatch({
          type: 'analyse_charge/updateUseCond',
          payload: values
        })
      }
    });
  }

  handleChange = (type, value) => {
    const { cond = {}, dispatch } = this.props;
    cond[type] = value;

    dispatch({
      type: 'analyse_charge/updateUseCond',
      payload: cond
    })
  }

  render() {
    const { form, dict = {} } = this.props;
    const { getFieldDecorator } = form;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
    };

    return (
      <Form onSubmit={this.search}>
        <SearchRow>
          <SearchCol key={0} span={6}>
            <FormItem
              label="站点名称"
              {...formItemLayout}
            >
              {getFieldDecorator('station')(
                <Input placeholder='站点名称' allowClear />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={1} span={6}>
            <FormItem
              label="充电桩名称"
              {...formItemLayout}
            >
              {getFieldDecorator('name')(
                <Input placeholder='充电桩名称' allowClear />
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={5} span={6}>
            <FormItem
              label="使用状态"
              {...formItemLayout}
            >
              {getFieldDecorator('status')(
                DropDownSelectObj({ dict, type: 'device.charging.status', onChange: (v) => this.handleChange('status', +v) })
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={6} span={4}>
            <SearchButton onClick={() => {
              window.history.back();
            }}>返回</SearchButton>
            <SearchButton type="primary" htmlType="submit" style={{ 'marginRight': '15px' }}>搜索</SearchButton>
          </SearchCol>
        </SearchRow>
      </Form>
    );
  }
}

const SearchS = Form.create<IProps>()(Search);
export default SearchS;
